iT邦幫忙

2022 iThome 鐵人賽

DAY 18
0
Mobile Development

Android Studio - 30天菜鳥筆記系列 第 18

Android Studio菜鳥筆記 - Day18 - ScrollView

  • 分享至 

  • xImage
  •  

ScrollView是當文件或圖片需大量顯示內容而造成螢幕無法完全顯示時,將內容變成可滾動式來讀取後面無法完全顯示內容。
屬性:
android:orientation 設定水平滾動或是垂直滾動
android:scrollbars="none" 隱藏滑塊
下面是個簡單的範例:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="350dp"
        android:layout_height="300dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.491"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.078">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Button
                android:id="@+id/button1"
                android:layout_width="300dp"
                android:layout_height="150dp"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:background="#FF1717"
                android:text="內容1"
                android:textColor="#000000"
                android:textSize="25dp" />
                        這裡都是Button
                		.
                		.
                		.
                		.            
                            
        </LinearLayout>
    </ScrollView>

    <HorizontalScrollView
        android:layout_width="350dp"
        android:layout_height="300dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.491"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.877">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button12"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:text="檔案A"
                android:textSize="25dp" />
                   這裡都是Button    
                        .
                        .
                        .
                        .            
            
        </LinearLayout>
    </HorizontalScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.scrollview_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button button1, button2, button3, button4,button5,button6,button7,button8,button9,button10,button11,button12;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);
        button5 = findViewById(R.id.button5);
        button6 = findViewById(R.id.button6);
        button7 = findViewById(R.id.button7);
        button8 = findViewById(R.id.button8);
        button9 = findViewById(R.id.button9);
        button10 = findViewById(R.id.button10);
        button11 = findViewById(R.id.button11);
        button12 = findViewById(R.id.button12);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
        button8.setOnClickListener(this);
        button9.setOnClickListener(this);
        button10.setOnClickListener(this);
        button11.setOnClickListener(this);
        button12.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                Toast.makeText(this, button1.getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.button2:
                Toast.makeText(this, button2.getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.button3:
                Toast.makeText(this, button3.getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.button4:
                Toast.makeText(this, button4.getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.button5:
                Toast.makeText(this, button5.getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.button6:
                Toast.makeText(this, button6.getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.button7:
                Toast.makeText(this, button7.getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.button8:
                Toast.makeText(this, button8.getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.button9:
                Toast.makeText(this, button9.getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.button10:
                Toast.makeText(this, button10.getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.button11:
                Toast.makeText(this, button11.getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.button12:
                Toast.makeText(this, button12.getText(), Toast.LENGTH_SHORT).show();
                break;

        }
    }
}

上一篇
Android Studio菜鳥筆記 - Day17 - ListView
下一篇
Android Studio菜鳥筆記 - Day19 - Intent
系列文
Android Studio - 30天菜鳥筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言